package com.wlhse.util;

import com.wlhse.dao.ProblemDescriptionDao;
import com.wlhse.entity.ProblemDescriptionPojo;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class PoiProblemDescriptions {

    @Resource
    private ProblemDescriptionDao dao;

    @Resource
    private SortCodeUtil util;

    public List<ProblemDescriptionPojo> getBeanList(Workbook wb) throws CloneNotSupportedException {
        List<ProblemDescriptionPojo> list = new ArrayList<>();
        ProblemDescriptionPojo pojo = new ProblemDescriptionPojo();

        String code = dao.queryLastCode();

        //遍历sheet
        for (int rowNum = 0; rowNum < wb.getNumberOfSheets(); rowNum++) {
            Sheet sheet = wb.getSheetAt(rowNum);
            //遍历行
            for (int j = 2; j < sheet.getPhysicalNumberOfRows(); j++) {
                Row row = sheet.getRow(j);
                ProblemDescriptionPojo tmp = (ProblemDescriptionPojo) pojo.clone();

                if (null==row.getCell(1)||null==row.getCell(2))
                    continue;
                if (StringUtils.isBlank(row.getCell(1).getStringCellValue()))
                    continue;
                if (StringUtils.isBlank(row.getCell(2).getStringCellValue()))
                    continue;
                code = util.getMaxCodeString(code);
                tmp.setProblemDescriptionPojo(code,row.getCell(1).getStringCellValue(),row.getCell(2).getStringCellValue());
                list.add(tmp);

            }
        }
        return list;
    }
}
